package day20170416.io.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;

public class InputOutStream {
	
	@Test
	public void testInputStream() throws Exception {
		inputStream("C:\\Users\\Administrator\\Desktop\\1.jpg", "2.jpg");
	}
	/**
	 * 字节流
	 * @throws Exception
	 */
	public void inputStream(String src,String dest){
		//文件的路径
		File out = new File(src);
		File in = new File(dest);
		//选择流
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			//讲文件路径装到管道
			fis = new FileInputStream(out);
			fos = new FileOutputStream(in);
			byte[] b = new byte[10];
			int len;
			//重点：读取到最后一个字节就停止
			while((len = fis.read(b)) != -1){
				//写入子节点，从0下标开始写
				fos.write(b, 0, len);
				fos.flush();
			}
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
